home *** CD-ROM | disk | FTP | other *** search
-
-
- /*
- *
- showbox.cpp
-
- code to support the class 'Box'
- *
- * $Revision: 1.7 $
- *
- *
- * $Log: C:/pbmdtsts/library/showbox.cpv $
- *
- * Rev 1.7 03 Jun 1993 14:25:36 C_BAKER
- * un-defined the HEAPTRACE define.
- *
- * Rev 1.6 02 Jun 1993 11:55:16 C_BAKER
- * Many changes.
- * Changes include;
- * removal of the concept of having a list of malloc'd
- * storage. The list was being overrun when multiple
- * calls to redraw were executed.
- *
- * Instead of using malloc and free there are now functions
- * GetStorage and KillStorage which allow greater debugging
- * ability.
- *
- *
- *
- * Rev 1.5 27 May 1993 14:31:02 C_BAKER
- * Removed the overloaded ErrorBox constructor and made the
- * 'ContinuousFlag' variable globel in mces.h.
-
- Rev 1.4 27 May 1993 13:24:20 C_BAKER
- Added overloaded ErrorBox constructor which takes a variable
- named 'Continuous'. This flag determines if the function will
- wait for keyboard input or just pause on error.
-
- Rev 1.3 20 May 1993 11:07:02 C_BAKER
-
- This revision appears to clear up the problem of the
- heap not being restored properly when the class is destroyed.
-
- Rev 1.2 19 May 1993 15:19:48 C_BAKER
- Fixed problems with malloc/free.
-
- Rev 1.1 19 May 1993 13:26:48 C_BAKER
- Added subclass 'ErrorBox'.
- Having trouble with running out of heap space due to
- fragmented mallocs and frees.
-
- Rev 1.0 10 May 1993 14:52:14 C_BAKER
- Initial revision.
- *
- */
-
- #include <string.h>
- #include <stdlib.h>
- #include "mces.h"
- #include "screen.h"
-
- char ShowMsgBox( char * Msg, int ErrOrPrompt );
- void NextPos( int &, int &, int );
- void ErrorTones(void);
-
- uchar * GetStorage( unsigned size );
- void KillStorage( char * tstore );
-
- // #define HEAPREPORT
-
- // #define HEAPTRACE
-
- // #define DONOTHING
-
-
- #ifdef HEAPREPORT
-
- typedef struct {
- int count;
- long size;
- } THEAP;
-
- long HeapSize(THEAP & theap);
- #endif
-
- #include "showbox.hpp"
-
- Box :: Box ( char * Title, char * Msg, int ReqXpos, int ReqYpos, uchar ReqAttrib )
- {
- #ifdef DONOTHING
- printf("\nBox message is %s", Msg );
- return;
- #endif
-
- BoxWidth = 0;
-
- AbsPos = 1;
-
- // set all message addresses to 0
- for (BreakLineInx = 0; BreakLineInx < 20; BreakLineInx++)
- Brokenlines[BreakLineInx] = 0;
-
- Xpos = ReqXpos; Ypos = ReqYpos; Attrib = ReqAttrib;
-
- BaseScreen = (unsigned char far *) 0xb8000000;
-
- LMsg = GetStorage(strlen(Msg)+5);
-
- if (!LMsg)
- {
- printf("\nUnable to allocate storage at box constructor\n"
- "File %s, line %u\n"
- "Press any key to continue",__FILE__, __LINE__);
- while (!kbhit());
- getchar();
- }
-
- LTitle = GetStorage(strlen(Title)+5);
- if (!LTitle)
- {
- printf("Could not allocate storage during creation of box\n"
- "File %s, line %u\n"
- "Press any key to continue",__FILE__, __LINE__);
- while (!kbhit());
- getchar();
- return;
- }
-
- strcpy( LTitle, Title );
-
- // if a system configuration verify error
- if (Msg[0] == 'S' && Msg[1] == 'V')
- strcpy( LMsg, &Msg[2] );
- else
- strcpy( LMsg, Msg );
-
- BreakupMessage(); // determines box with
-
- SaveScreenArea();
-
- BuildTheBox();
-
- FillTheBox();
-
- MessageInBox();
-
- TitleOnBox();
-
- ShadeTheBox();
-
- // free allocated storage
- for (BreakLineInx = 0; Brokenlines[BreakLineInx]; BreakLineInx++)
- {
- KillStorage ( Brokenlines[BreakLineInx] );
- Brokenlines[BreakLineInx] = 0;
- }
- }
-
- Box :: Box ( char * Title, char * Msg, uchar ReqAttrib )
- {
- #ifdef DONOTHING
- printf("\nBox message is %s", Msg );
- return;
- #endif
-
- BoxWidth = 0;
-
- AbsPos = 0;
-
- NextPos(Xpos, Ypos, 3);
-
- // set all message addresses to 0
- for (BreakLineInx = 0; BreakLineInx < 20; BreakLineInx++)
- Brokenlines[BreakLineInx] = 0;
-
- Attrib = ReqAttrib;
-
- BaseScreen = (unsigned char far *) 0xb8000000;
-
- LMsg = GetStorage(strlen(Msg)+5);
- if (!LMsg)
- {
- printf("\nUnable to allocate storage at box constructor\n"
- "File %s, line %u\n"
- "Press any key to continue",__FILE__, __LINE__);
- while (!kbhit());
- getchar();
- }
-
- LTitle = GetStorage(strlen(Title)+5);
- if (!LTitle)
- {
- printf("Could not allocate storage during creation of box\n"
- "File %s, line %u\n"
- "Press any key to continue",__FILE__, __LINE__);
- while (!kbhit());
- getchar();
- return;
- }
-
- strcpy( LTitle, Title );
-
- // if a system configuration verify error
- if (Msg[0] == 'S' && Msg[1] == 'V')
- strcpy( LMsg, &Msg[2] );
- else
- strcpy( LMsg, Msg );
-
- BreakupMessage(); // determines box with
-
- SaveScreenArea();
-
- BuildTheBox();
-
- FillTheBox();
-
- MessageInBox();
-
- TitleOnBox();
-
- ShadeTheBox();
-
- // free allocated storage
- for (BreakLineInx = 0; Brokenlines[BreakLineInx]; BreakLineInx++)
- {
- KillStorage ( Brokenlines[BreakLineInx] );
- Brokenlines[BreakLineInx] = 0;
- }
- }
-
-
- Box :: Box ( char * Title, int ReqXpos, int ReqYpos, uchar ReqAttrib )
- {
- #ifdef DONOTHING
- printf("\nBox message is NO-MESSAGE" );
- return;
- #endif
- BoxWidth = 0;
-
- SavedScreenArea = 0;
-
- AbsPos = 1;
-
- // set all message addresses to 0
- for (BreakLineInx = 0; BreakLineInx < 20; BreakLineInx++)
- Brokenlines[BreakLineInx] = 0;
-
- Xpos = ReqXpos; Ypos = ReqYpos; Attrib = ReqAttrib;
-
- BaseScreen = (unsigned char far *) 0xb8000000;
-
- setmem( Brokenlines, sizeof(Brokenlines), 0 );
-
- LTitle = GetStorage(strlen(Title)+5);
- if (!LTitle)
- {
- printf("Could not allocate storage during creation of box\n"
- "File %s, line %u\n"
- "Press any key to continue",__FILE__, __LINE__);
- while (!kbhit());
- getchar();
- return;
- }
-
- strcpy( LTitle, Title );
-
- LMsg = 0;
-
- // free allocated storage
- for (BreakLineInx = 0; Brokenlines[BreakLineInx]; BreakLineInx++)
- {
- KillStorage ( Brokenlines[BreakLineInx] );
- Brokenlines[BreakLineInx] = 0;
- }
- }
-
- Box :: ~Box()
- {
- #ifdef DONOTHING
- return;
- #endif
- if (SavedScreenArea)
- {
- RestoreScreenArea();
- KillStorage( SavedScreenArea );
- }
-
- if (!AbsPos)
- NextPos(Xpos, Ypos, -3);
-
- KillStorage( LMsg );
- KillStorage( LTitle );
-
- }
-
- void Box :: SaveScreenArea(void)
- {
- SavedScreenArea = GetStorage(4000); // one screen's worth char & attr
-
- if (!SavedScreenArea)
- {
- printf("Unable to allocate storage to save the screen area\n"
- "File %s, line %u\n"
- "Press any key to continue",__FILE__, __LINE__);
- while (!kbhit());
- getchar();
- }
-
-
- char *ScrnPtr = SavedScreenArea;
-
- for (int y = 0; y < BoxHite + 1; y++)
- for (int x = 0; x < BoxWidth + 1; x++)
- {
- WorkScreen = BaseScreen + (Xpos + x) * 2 + (Ypos + y) * 160;
- *ScrnPtr++ = *WorkScreen++;
- *ScrnPtr++ = *WorkScreen++;
- }
-
- }
-
- void Box :: RestoreScreenArea(void)
- {
- char *ScrnPtr = SavedScreenArea;
-
- for (int y = 0; y < BoxHite + 1; y++)
- for (int x = 0; x < BoxWidth + 1; x++)
- {
- WorkScreen = BaseScreen + (Xpos + x) * 2 + (Ypos + y) * 160;
- *WorkScreen++ = *ScrnPtr++;
- *WorkScreen++ = *ScrnPtr++;
- }
-
- }
-
- void Box :: BuildTheBox(void)
- {
-
- WorkScreen = BaseScreen + Xpos * 2 + Ypos * 160;
-
- *WorkScreen++ = '╔';
- *WorkScreen++ = Attrib;
-
- for (int x = 1; x < BoxWidth - 1; x++)
- {
- *WorkScreen++ = '═';
- *WorkScreen++ = Attrib;
- }
-
- *WorkScreen++ = '╗';
- *WorkScreen++ = Attrib;
-
- WorkScreen = BaseScreen + Xpos * 2 + (Ypos + BoxHite - 1) * 160;
-
- *WorkScreen++ = '╚';
- *WorkScreen++ = Attrib;
-
- for ( x = 1; x < BoxWidth - 1; x++)
- {
- *WorkScreen++ = '═';
- *WorkScreen++ = Attrib;
- }
-
- *WorkScreen++ = '╝';
- *WorkScreen++ = Attrib;
-
- for (int y = 1; y < BoxHite - 1; y++)
- {
- WorkScreen = BaseScreen + Xpos * 2 + (Ypos + y) * 160;
- *WorkScreen++ = '║';
- *WorkScreen++ = Attrib;
-
- WorkScreen = BaseScreen + Xpos * 2 + (BoxWidth - 1) * 2 + (Ypos + y) * 160;
- *WorkScreen++ = '║';
- *WorkScreen++ = Attrib;
-
- }
-
- }
-
- void Box :: MessageInBox(void)
- {
- int inx;
- char * p1;
-
- for ( inx = 0; Brokenlines[inx]; inx++ )
- {
- WorkScreen = BaseScreen + (Xpos + 2) * 2 + (Ypos + 2 + inx) * 160;
-
- for ( p1 = Brokenlines[inx]; *p1; p1++ )
- {
- *WorkScreen++ = *p1;
- *WorkScreen++ = Attrib;
- }
- }
- }
-
- void Box :: TitleOnBox(void)
- {
- WorkScreen = BaseScreen + (Xpos + 2) * 2 + Ypos * 160;
-
- for ( char * p1 = LTitle; *p1; p1++ )
- {
- *WorkScreen++ = *p1;
- *WorkScreen++ = Attrib;
- }
- }
-
- void Box :: FillTheBox(void)
- {
- for (int y = 1; y < BoxHite - 1; y++ )
- for (int x = 1; x < BoxWidth - 1; x++)
- {
- WorkScreen = BaseScreen + (Xpos + x) * 2 + (Ypos + y) * 160;
- *WorkScreen++ = '░';
- *WorkScreen = Attrib;
- }
- }
-
- void Box :: ShadeTheBox(void)
- {
- for (int y = 1; y <= BoxHite; y++)
- {
- WorkScreen = BaseScreen + (Xpos + BoxWidth ) * 2 + (Ypos + y) * 160;
- *(WorkScreen + 1) = 8; // set attribute
- }
-
- for (int x = Xpos + 1; x <= Xpos + BoxWidth; x++ )
- {
- WorkScreen = BaseScreen + x * 2 + (Ypos + BoxHite) * 160;
- *(WorkScreen + 1) = 8; // set attribute
- }
-
- }
-
- void Box :: BreakupMessage(void)
- {
-
- int BreakLineInx;
- char *pWork; // holds position during long string break-up
- int MsgLen;
-
- BreakLineInx = 0;
- pWork = LMsg;
-
- do
- {
- Brokenlines[BreakLineInx] = GetStorage(100);
- if (Brokenlines[BreakLineInx] == NULL)
- {
- printf("\nUnable to allocate storage to break up message\n"
- "File %s, line %u\n"
- "Press any key to continue",__FILE__, __LINE__);
- while (!kbhit());
- getchar();
- }
-
- setmem( Brokenlines[BreakLineInx], 60, 0 );
-
- // put as big as piece of the message as possible
- // into the malloc'd storage
- BreakLine( BreakLineInx, &pWork );
-
- MsgLen = strlen( pWork );
- BreakLineInx++;
-
- } while( MsgLen );
-
- BoxHite = BreakLineInx + 4;
-
- BoxWidth = max ( BoxWidth, strlen( LTitle ) + 4 );
- }
-
- /**********************************************************************
- * BoxWidth is set to 0 before calling
- * to break up long lines
- *
- *if length of LMsg <= 56 && !(strchr(LMsg, '\n') )
- * copy LMsg to element of buffer[]
- * set return pointer to end of LMsg
- * set BoxWidth to length of LMsg + 4
- * return
- *
- *if strchr( LMsg, '\n' )
- * if the '\n' is lte 56 chars from beginning of LMsg
- * copy from beginning of LMsg up to the '\n'
- * into element of buffer
- * set BoxWidth to the max of BoxWidth or numchars
- * from beginning of LMSG up to current position
- * set return pointer to position of '\n' + 1
- * return
- * else
- * step back from '\n' until a space is encountered
- * and the pointer is lte LMsg + 56
- * set BoxWidth to the max of BoxWidth or numchars
- * from beginning of LMSG up to current position
- * set return pointer to current position + 1
- * return
- *
- * remaining condition is that the string has no '\n' in it
- * and it is longer then 56 chars
- *
- *set pointer to LMsg + 56
- *step back until a space is reached
- *copy from LMsg up to current position
- *set BoxWidth to the max of BoxWidth or numchars
- * from beginning of LMSG up to current position
- *set return pointer to current position + 1
- *
- *end of function to break up long lines
- ************************************************************/
-
- void Box :: BreakLine ( int inx, char ** FirstPtr )
- {
- char * CurrentPosition;
- int LMLen;
-
- LMLen = strlen(*FirstPtr);
-
- if ( LMLen <= 56 && !strchr(*FirstPtr,'\n') )
- {
- strcpy( Brokenlines[inx], *FirstPtr );
- *FirstPtr = LMLen + *FirstPtr;
- BoxWidth = max(BoxWidth, LMLen + 4);
- return;
- }
-
- if ( (CurrentPosition = strchr( *FirstPtr, '\n')) != 0 )
- {
- if ( (CurrentPosition - *FirstPtr) <= 56 )
- {
- strncpy(Brokenlines[inx], *FirstPtr, CurrentPosition - *FirstPtr );
- BoxWidth = max(BoxWidth, CurrentPosition - *FirstPtr + 4 );
- *FirstPtr = CurrentPosition + 1;
- return;
- }
- else
- {
- while (*CurrentPosition != ' ' || (CurrentPosition - *FirstPtr) > 56 )
- CurrentPosition--;
-
- strncpy(Brokenlines[inx], *FirstPtr, CurrentPosition - *FirstPtr );
- BoxWidth = max(BoxWidth, CurrentPosition - *FirstPtr + 4 );
- *FirstPtr = CurrentPosition + 1;
- return;
- }
- }
-
- // remaining condition is that the string has no '\n' in it
- // and it is longer then 56 chars
-
- CurrentPosition = *FirstPtr + 56;
- while (*CurrentPosition != ' ' )
- CurrentPosition--;
-
- strncpy(Brokenlines[inx], *FirstPtr, CurrentPosition - *FirstPtr );
-
- BoxWidth = max(BoxWidth, CurrentPosition - *FirstPtr + 4 );
-
- *FirstPtr = CurrentPosition + 1;
-
- }
-
- void Box :: Redraw( char * Msg )
- {
- #ifdef DONOTHING
- printf("\nRedraw message is %s", Msg );
- return;
- #endif
-
- // set all message addresses to 0
- for (BreakLineInx = 0; BreakLineInx < 20; BreakLineInx++)
- Brokenlines[BreakLineInx] = 0;
-
- if ( strlen(Msg) <= strlen(LMsg) )
- {
- setmem( LMsg, strlen(LMsg), '░' );
-
- BreakupMessage(); // determines box with
- MessageInBox();
- for (BreakLineInx = 0; Brokenlines[BreakLineInx]; BreakLineInx++)
- {
- KillStorage ( Brokenlines[BreakLineInx] );
- Brokenlines[BreakLineInx] = 0;
- }
-
- strcpy( LMsg, Msg );
-
- BreakupMessage(); // determines box with
- // FillTheBox();
- MessageInBox();
- // free allocated storage
- for (BreakLineInx = 0; Brokenlines[BreakLineInx]; BreakLineInx++)
- {
- KillStorage ( Brokenlines[BreakLineInx] );
- Brokenlines[BreakLineInx] = 0;
- }
- return;
- }
-
- if (SavedScreenArea)
- {
- RestoreScreenArea();
- KillStorage( SavedScreenArea );
- }
-
- KillStorage(LMsg);
-
- LMsg = GetStorage(strlen(Msg)+5);
-
- if (!LMsg)
- {
- printf("\nCould not allocate storage during creation of box\n"
- "File %s, line %u\n"
- "Press any key to continue",__FILE__, __LINE__);
- while (!kbhit());
- getchar();
- return;
- }
-
- strcpy( LMsg, Msg );
-
- BreakupMessage(); // determines box with
-
- SaveScreenArea();
-
- BuildTheBox();
-
- FillTheBox();
-
- MessageInBox();
-
- TitleOnBox();
-
- ShadeTheBox();
-
- // free allocated storage
- for (BreakLineInx = 0; Brokenlines[BreakLineInx]; BreakLineInx++)
- {
- KillStorage ( Brokenlines[BreakLineInx] );
- Brokenlines[BreakLineInx] = 0;
- }
- }
-
- void NextPos( int &xpos, int &ypos, int incdec )
- {
- static xp, yp, firsttime;
-
- if (!xpos && !ypos && !incdec)
- {
- firsttime = 0;
- return;
- }
-
- if (!firsttime)
- {
- xp = 10;
- yp = 5;
- firsttime++;
- xpos = xp += incdec;
- ypos = yp += incdec;
- return;
- }
-
- if (xp >= 10)
- xpos = xp += incdec;
- if (yp >= 5)
- ypos = yp += incdec;
- }
-
- ErrorBox :: ErrorBox( char * Msg )
- : Box( "ERROR",Msg,BRIGHTWHITEFORGROUND + REDBACKGROUND )
- {
- char * lline = GetStorage(200);
-
- if (!lline)
- {
- printf("\nTrouble allocating storage at %s %u",
- __FILE__, __LINE__);
- while (!kbhit());
- getch();
- }
-
- strcpy(lline,Msg);
-
- if (!ContinuousFlag)
- strcat(lline,"\n\nHit any key to continue");
-
- Redraw( lline );
-
- ErrorTones();
-
- if (!ContinuousFlag)
- {
- while (!kbhit());
- getch();
- }
- else
- sleep(3);
-
- KillStorage (lline);
- }
-
- // #define WAIT
-
- uchar * GetStorage( unsigned size )
- {
- char * tstore;
-
- #ifdef HEAPREPORT
- THEAP Lheap = {0,0};
-
- if( HeapSize( Lheap ) == -1L )
- {
- printf("\nHeap error");
- while (!kbhit());
- getch();
- }
-
- printf("\nHeap count at getstorage1 = %u, heap size = %lu",Lheap.count, Lheap.size );
- #ifdef WAIT
- while(!kbhit());
- getch();
- #endif
- #endif
-
- // tstore = (char *) malloc( size );
- tstore = new uchar[ size ];
-
- #ifdef HEAPTRACE
- printf("\nAllocating %p\tsize %u", tstore, size );
- #endif
-
- if (!tstore)
- {
- printf("\nUnable to allocate storage");
- while (!kbhit());
- getch();
- }
-
- #ifdef HEAPREPORT
- THEAP mheap = {0,0};
-
- if( HeapSize( mheap ) == -1L )
- {
- printf("\nHeap error");
- while (!kbhit());
- getch();
- }
-
- printf("\nHeap count at getstorage2 = %u, heap size = %lu",mheap.count, mheap.size );
- #ifdef WAIT
- while(!kbhit());
- getch();
- #endif
- #endif
-
- return tstore;
-
- }
-
- void KillStorage( char * tstore )
- {
- #ifdef HEAPREPORT
- THEAP Lheap = {0,0};
- if( HeapSize( Lheap ) == -1L )
- {
- printf("\nHeap error");
- while (!kbhit());
- getch();
- }
-
- printf("\nHeap count at killstorage1 = %u, heap size = %lu",Lheap.count, Lheap.size );
- #ifdef WAIT
- while(!kbhit());
- getch();
- #endif
- #endif
-
- #ifdef HEAPTRACE
- printf("\nFreeing %p", tstore );
- #endif
-
- // free( tstore );
- delete( tstore );
-
- #ifdef HEAPREPORT
- Lheap.size = 0;
- Lheap.count = 0;
-
- if( HeapSize( Lheap ) == -1L )
- {
- printf("\nHeap error");
- while (!kbhit());
- getch();
- }
-
- printf("\nHeap count at killstorage2 = %u, heap size = %lu",Lheap.count, Lheap.size );
- #ifdef WAIT
- while(!kbhit());
- getch();
- #endif
- #endif
-
- }
-